package edu.gatech.app4args;

import edu.gatech.app4args.resources.ResourceLoader;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * Represents a SlideData object in the OverviewPanel. Shows the slide number,
 * type, and tag (eventually) of the slide.
 *
 * @author Andrew Lunsford
 */
public class OverviewSlide extends JPanel{
	/** Reference to Main */
	private Main ref;
	/** The label that displays the slide number and type icon */
	private JLabel label;
	/** A label to show the slide's tag */
	private JLabel tag;

	/**
	 * Creates a new OverviewSlide and sets its variables and layout.
	 *
	 * @param ref a reference to Main
	 * @param slidetype the type of slide icon to load
	 * @param slideNumber the slide number to display
	 */
	public OverviewSlide(Main ref, int slidetype, int slideNumber){
		this.setMaximumSize(new Dimension(65,85));
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		this.ref = ref;
		label = new JLabel();
		tag = new JLabel();
		
		setup(slidetype, slideNumber);
	}

	/**
	 * Sets up the OverviewSlide by setting the label with the proper slide
	 * number and icon, and initializing the tags. Adds a mouse listener to
	 * call a change of selected slide in Main.
	 *
	 * @param slidetype the type of slide icon to use
	 * @param slideNumber the number to display in the label
	 */
	private void setup(int slidetype, int slideNumber){
        label.setName("OS "+slideNumber+" Label");
		label.setText(Integer.toString(slideNumber));
		label.setFont(new Font("Tahoma", Font.BOLD, 11));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.TOP);
		label.setIcon(ResourceLoader.slideIcon(slidetype));

		tag.setName("OS "+slideNumber+" Tag");
		//tag.setText("tags bro");
		tag.setFont(new Font("Tahoma", Font.BOLD, 11));
        tag.setHorizontalAlignment(SwingConstants.CENTER);

		// TODO switch to box/absolute layout
		GroupLayout overviewSlideLayout = new GroupLayout(this);
        this.setLayout(overviewSlideLayout);
		
        overviewSlideLayout.setHorizontalGroup(
            overviewSlideLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(label, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
            .addComponent(tag, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
        );
        overviewSlideLayout.setVerticalGroup(
            overviewSlideLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(overviewSlideLayout.createSequentialGroup()
                .addComponent(label, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                .addGap(4,4,4)
                .addComponent(tag))
        );

		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent me){
				if(me.getButton()== MouseEvent.BUTTON1){
					ref.slideSelected(Integer.parseInt(label.getText())-1);
				}
			}
		});

		this.revalidate();
	}


}
